package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.FamilyManagementActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.FamilyManagementModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/2.
 */

public class FamilyManagementAdapter extends BaseAdapter {

    private Context mContext;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    List<FamilyManagementModel> model= new ArrayList<>();
    public FamilyManagementAdapter(Context context) {
        this.mContext = context;
        options = ImgUtils.imageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void addItem( List<FamilyManagementModel> model){

        this.model=model;
        notifyDataSetChanged();
    }
    public void adddeleting(){

        model.clear();
        notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_family_management, null);
            holder.item_content = (TextView) convertView
                    .findViewById(R.id.item_content);
            holder.item_title = (TextView) convertView
                    .findViewById(R.id.item_title);
            holder.item_img = (ImageView) convertView
                    .findViewById(R.id.item_img);
            holder.delete = (TextView) convertView
                    .findViewById(R.id.delete);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final FamilyManagementModel familyManagementModel = model.get(position);
        imageLoader.displayImage(Constant.HTTP + familyManagementModel.getPicimage(),  holder.item_img , options);
        holder.item_title.setText("套内"+familyManagementModel.getArea()+"㎡户型");
        holder.item_content.setText(familyManagementModel.getFtype());
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((FamilyManagementActivity)mContext).processdeleting(familyManagementModel.getId(),familyManagementModel.getPicimage());
            }
        });
        return convertView;
    }

    class ViewHolder {


        TextView item_title;
        TextView item_content;
        ImageView item_img;
        TextView delete;

    }
}


